{
 "metadata": {
  "signature": "sha256:a40a97434f3e2d03a8376ba123e914b72a70d398f8ed15e7c380c13f13421837"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: drag'n'drop text example\n",
      "======================================================================\n",
      "\n",
      "introduction\n",
      "------------\n",
      "\n",
      "Matplotlib provides event handling to determine things like key presses,\n",
      "mouse position, and button clicks. Matplotlib supports a number of GUIs,\n",
      "and provides an interface to the GUI event handling via the mpl\\_connect\n",
      "and mpl\\_disconnect methods.\n",
      "\n",
      "This page gives an example of use of these facilities by adding a\n",
      "Drag'n'Drop handler for text objects. You can get the source code for\n",
      "this example here: [Text_DragnDrop_v0.1.py](files/attachments/Matplotlib_Drag_n_Drop_Text_Example/Text_DragnDrop_v0.1.py) .)\n",
      "\n",
      "Defining the handler class\n",
      "--------------------------"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python numbers=disable\n",
      "from matplotlib         import pylab as p\n",
      "from matplotlib.text\timport Text\n",
      "\n",
      "class DragHandler(object):\n",
      "    \"\"\" A simple class to handle Drag n Drop.\n",
      "\n",
      "    This is a simple example, which works for Text objects only\n",
      "    \"\"\"\n",
      "    def __init__(self, figure=None) :\n",
      "        \"\"\" Create a new drag handler and connect it to the figure's event system.\n",
      "        If the figure handler is not given, the current figure is used instead\n",
      "        \"\"\"\n",
      "\n",
      "        if figure is None : figure = p.gcf()\n",
      "        # simple attibute to store the dragged text object\n",
      "        self.dragged = None\n",
      "\n",
      "        # Connect events and callbacks\n",
      "        figure.canvas.mpl_connect(\"pick_event\", self.on_pick_event)\n",
      "        figure.canvas.mpl_connect(\"button_release_event\", self.on_release_event)\n",
      "\n",
      "    def on_pick_event(self, event):\n",
      "        \" Store which text object was picked and were the pick event occurs.\"\n",
      "\n",
      "        if isinstance(event.artist, Text):\n",
      "            self.dragged = event.artist\n",
      "            self.pick_pos = (event.mouseevent.xdata, event.mouseevent.ydata)\n",
      "        return True\n",
      "\n",
      "    def on_release_event(self, event):\n",
      "        \" Update text position and redraw\"\n",
      "\n",
      "        if self.dragged is not None :\n",
      "            old_pos = self.dragged.get_position()\n",
      "            new_pos = (old_pos[0] + event.xdata - self.pick_pos[0],\n",
      "                       old_pos[1] + event.ydata - self.pick_pos[1])\n",
      "            self.dragged.set_position(new_pos)\n",
      "            self.dragged = None\n",
      "            p.draw()\n",
      "        return True"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "A small use case\n",
      "----------------"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#! python numbers=disable\n",
      "\n",
      "# Usage example\n",
      "from numpy import *\n",
      "\n",
      "# Create arbitrary points and labels\n",
      "x, y = random.normal(5, 2, size=(2, 9))\n",
      "labels = [ \"Point %d\" % i for i in xrange(x.size)]\n",
      "\n",
      "# trace a scatter plot\n",
      "p.scatter(x, y)\n",
      "p.grid()\n",
      "\n",
      "# add labels and set their picker attribute to True\n",
      "for a,b,l in zip(x,y, labels):\n",
      "    p.text(a, b, l, picker=True)\n",
      "\n",
      "# Create the event hendler \n",
      "dragh = DragHandler()\n",
      "\n",
      "p.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The Text objects can now be moved with the mouse."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}